package crm.view.popup;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;

import crm.model.teacherPC.Manager;
import crm.model.teacherPC.connectList.ConnectStudent;



public class ConnectPopup extends JPopupMenu implements ActionListener {
	private static ConnectPopup popup;

	private Manager manager = null;
	private ConnectStudent student = null;
	static {
		popup = new ConnectPopup();
	}
	public static void show(Manager manager, ConnectStudent student, Component c, int x, int y){
		popup.showPopUp(manager,student, c, x, y);
	}
	public void showPopUp(Manager manager, ConnectStudent student, Component c, int x, int y){
		this.manager = manager;
		this.student=student;
		show(c,x,y);
	}
	public ConnectPopup() {
		setBorder(new BevelBorder(BevelBorder.RAISED));
		JMenuItem item = new JMenuItem("connect");
		item.addActionListener(this);
		add(item);
		item = new JMenuItem("disconnect");
		item.addActionListener(this);
		add(item);
		item = new JMenuItem("shutdown");
		item.addActionListener(this);
		add(item);
		item = new JMenuItem("turn on");
		item.addActionListener(this);
		add(item);
		item = new JMenuItem("ping");
		item.addActionListener(this);
		add(item);
	}

	public void actionPerformed(ActionEvent ev) {
		if (ev.getActionCommand().equals("connect")) {
			manager.addStudent(student.connect(manager));

		} else if (ev.getActionCommand().equals("disconnect")) {
			student.getStudent().closeConnection();
			student.setConnected(false, null);

		}else if (ev.getActionCommand().equals("shutdown")){
			try {
				Runtime.getRuntime().exec("shutdown -s");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}else if (ev.getActionCommand().equals("turn on")) {


		}else if (ev.getActionCommand().equals("ping")) {
			try {
				Process p = Runtime.getRuntime().exec("cmd /c start ping " + student.getIP());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
}
